import Vue from 'vue'
import axios from 'axios'
import 'styles/loaninfo.css'
import $ from 'jquery'
import VueCookies from 'vue-cookies'
window.jQuery = $
window.$ = $
Vue.use(VueCookies)
var BASE_URL = 'http://rest.hzdj-finance.com/hzdj/weixin/'
var AUTH_URL = 'http://www.madfrog.cn/hzdj/'

var BASE_IMG_URL = 'http://115.29.189.253/img_upload/'
var BASE_TEST_STATUS = 'HZDJ_TEST'
var BASE_QR = 'wx.hzdj-finance.com'
var BASE_APPID = 'wx9b358c3ddf2ed6d7'
export default{
  BASE_URL,
  AUTH_URL,
  BASE_IMG_URL,
  BASE_TEST_STATUS,
  BASE_QR,
  BASE_APPID,
  hzdjUploading: function (event, name, max) {
    var allowTypes = [ 'image/jpg', 'image/jpeg', 'image/png', 'image/gif' ]
    var maxSize = 5 * 1024 * 1024
    var maxWidth = 300
    var files = event.target.files
    if (files.length === 0) {
      return
    }
    if ((files.length + $('.' + name + '_uploader_number').length) > max) {
      $.toast('最多只能上传' + max + '张图片', 'text')
      return
    }
    for (var i = 0, len = files.length; i < len; i++) {
      var file = files[i]
      var reader = new FileReader()
      if (allowTypes.indexOf(file.type) === -1) {
        $.toast('该类型不允许上传', 'text')
        continue
      }
      if (file.size > maxSize) {
        $.toast('图片太大，不允许上传', 'text')
        continue
      }
      if ($('.' + name + '_uploader_number').length >= max) {
        $.toast('最多只能上传' + max + '张图片', 'text')
        return
      }
      reader.onload = function (e) {
        var img = new Image()
        img.onload = function () {
          var w = Math.min(maxWidth, img.width)
          var h = img.height * (w / img.width)
          var canvas = document.createElement('canvas')
          var ctx = canvas.getContext('2d')
          canvas.width = w
          canvas.height = h
          ctx.drawImage(img, 0, 0, w, h)
          var base64 = canvas.toDataURL('image/png')
          var $preview = $('<li class="weui_uploader_file weui_uploader_status ' + name + '_uploader_number" style="background-image:url(' + base64 + ')"><div class="weui_uploader_status_content">0%</div></li>')
          $('.' + name + '_weui_uploader_files').append($preview)
          var num = $('.' + name + '_uploader_number').length
          $('.' + name + '_js_counter').text(num + '/' + max)
          var progress = 0
          function uploading () {
            $preview.find('.weui_uploader_status_content').text(++progress + '%')
            if (progress < 100) {
              setTimeout(uploading, 30)
            } else {
              $preview.removeClass('weui_uploader_status').find('.weui_uploader_status_content').remove()
            }
          }
          setTimeout(uploading, 30)
          var token = $cookies.get('token')
          console.log('coken:' + token)
          axios.post('http://testrest.hzdj-finance.com/hzdj/upload', {
            'imgData': base64
          }, {headers: {'Authorization': 'Bearer ' + token}}).then(response => {
            if (response.data === 'UPLOAD_FAIL') {
              $.toast('上传图片失败！', 'text')
            } else {
              $.toast('上传图片成功！', 'text')
              var picture = $('#' + name).val()
              if (picture === '') {
                $('#' + name).val(response.data)
              } else {
                $('#' + name + '').val(picture + '#' + response.data)
              }
            }
          })
        }
        img.src = e.target.result
      }
      reader.readAsDataURL(file)
    }
  },
  ID: function (event) {
    var mobile = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
    return mobile.test(event)
  },
  del: function (event, name, max) {
    $('#' + name).val('')
    $('#' + name + '_uploaderInput').val('')
    $('#' + name + 'Del').empty()
    $('.' + name + '_js_counter').text('0/' + max)
  },
  phoneNumber: function (phone) {
    var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/
    return mobile.test(phone)
  },
  getUrlParam: function (name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
    var number = window.location.hash.indexOf('?')
    var r = window.location.hash.substr(number + 1).match(reg)
    if (r != null) return unescape(r[2])
    return null
  }
}
